package com.fs.sale.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.page.PageDomain;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.core.vo.AjaxResult;
import com.fs.common.util.page.PageUtils;
import com.fs.sale.pojo.PriceList;
import com.fs.sale.service.PriceDetailedService;
import com.fs.sale.service.PriceListService;
import com.fs.sale.vo.OrderSearch;
import com.fs.sale.vo.PriceListItem;
import com.fs.sale.vo.PriceListVo;
import com.fs.system.mapper.SysUserMapper;
import com.fs.system.util.SecurityUtils;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("priceList")
public class PriceListController extends BaseController {

    @Autowired
    PriceListService priceListService;
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    PriceDetailedService priceDetailedService;


    @GetMapping("test")
    public String Test(){
        return "Yrg";
    }
    @GetMapping("userTest")
    public String userTest(){
        return null;
    }


    @PostMapping("add")
    public AjaxResult priceList(@RequestBody PriceList priceList){
        priceListService.addPriceList(priceList);
        return AjaxResult.success();
    }

    @PostMapping("select")
    public TableDataInfo select(@RequestBody OrderSearch orderSearch){
        PageDomain pageDomain= PageUtils.startPage();
        if(Objects.nonNull(orderSearch.getCurrentPage()))
            pageDomain.setPageNum(orderSearch.getCurrentPage());
        if(Objects.nonNull(orderSearch.getPageSize()))
            pageDomain.setPageSize(orderSearch.getPageSize());


       Page<PriceListVo> page=priceListService.select(pageDomain,orderSearch);

        return this.getDataTable(page) ;
    }

    @GetMapping("toExamine")
    public AjaxResult toExaminePriceList(@RequestParam(value = "plId",required = false) Integer plId,@RequestParam(value = "newStatus",required = false) Integer newStatus){
        if(priceListService.toExaminePriceList(plId,newStatus)>0){
            return AjaxResult.success();
        }else if(priceListService.toExaminePriceList(plId,newStatus)==null){
            return AjaxResult.error("您不具备这个权限");
        }
        return AjaxResult.error();
    }

    @PostMapping("selectUser")
    public AjaxResult selectUser(){
        AjaxResult ajaxResult=new AjaxResult();

        ajaxResult.put("sysUserList",sysUserMapper.selectAllOrdinaryEmployeesByDeptId(300));
        return ajaxResult;
    }


    @GetMapping("selectDetailed")
    public AjaxResult selectDetailed(Integer plId){
        AjaxResult ajaxResult=new AjaxResult();
       List<PriceListItem> priceListItems=priceDetailedService.selectDetailed(plId);
       ajaxResult.put("priceListItems",priceListItems);
       return ajaxResult;
    }

   

}
